package com.tecode.controller.frontend;



import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.tecode.bean.Code;
import com.tecode.controller.FavoriteController;
import com.tecode.controller.songSheet.SongSheetController;
import com.tecode.service.FavoriteService;
import com.tecode.service.MusicService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@RequestMapping("/music")
@RestController
public class MusicController {
    @Autowired
    private FavoriteService favoriteService;

    @Autowired
    private MusicService musicService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/code")
    public String getMusicCodeByCodeId(HttpSession session, HttpServletRequest req, HttpServletResponse resp, Model model){

        resp.setContentType("text/json");
        resp.setCharacterEncoding("utf-8");
        List<Object> musicList=new ArrayList<>();
        List<Code> list = musicService.getMusicCodeByCodeId(4);

        for (int i = 0; i < list.size() ; i++) {
            int id= list.get(i).getId();
            List<Map> objectList = musicService.selectMusicByCodeId(id);
            musicList.add(objectList);
        }
        System.out.println("getMusicCodeByCodeId");

        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        map.put("musicList",musicList);
        JSONArray jsonArray=JSONArray.fromObject(map);

        try {
            resp.getWriter().print(jsonArray);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    @RequestMapping("/play")
    public String getMusicListToPlay(HttpSession session, HttpServletRequest req, HttpServletResponse resp, Model model){

        resp.setContentType("text/json");
        resp.setCharacterEncoding("utf-8");
        Map<String,Object> map=new HashMap<>();
        String userId = req.getParameter("userId");
        Integer currIndex=0;

        List<Map> musicList = musicService.selectDefaultSheet();
        System.out.println(musicList);
        redisTemplate.opsForValue().set("musicList",musicList);

        System.out.println(musicList);
        List<Boolean> scList=new ArrayList<>();
        if (!userId.equals("undefined")){
            System.out.println("nouserId");
            Boolean sc=true;
            List<Map> objects = musicService.selectCollectionMusicByUserId(Integer.valueOf(userId));
            for (int i = 0; i <musicList.size() ; i++) {
                for (int j = 0; j <objects.size() ; j++) {
                    if (objects.get(j).get("musicId").equals(musicList.get(i).get("musicId"))){
                        sc=true;
                        break;
                    }else {
                        sc=false;
                    }
                }
                scList.add(sc);
            }
            System.out.println(scList);

        }

        map.put("musicList",musicList);
        map.put("currIndex",currIndex);
        map.put("scList",scList);
        JSONArray jsonArray=JSONArray.fromObject(map);

        try {
            resp.getWriter().print(jsonArray);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @RequestMapping("/playfresh")
    public String getMusicListToPlayFresh(HttpSession session, HttpServletRequest req, HttpServletResponse resp, Model model){

        resp.setContentType("text/json");
        resp.setCharacterEncoding("utf-8");
        Map<String,Object> map=new HashMap<>();
        Integer currIndex=0;
        List<Boolean> scList=new ArrayList<>();



        Integer musicId = Integer.valueOf(req.getParameter("musicId"));
        String userId = req.getParameter("userId");


        System.out.println(userId+"-------------");




        List<Map> musicList = (List<Map>) redisTemplate.opsForValue().get("musicList");

        if (musicId >0){
            List<Map> maps = musicService.selectMusicByMusicId(musicId);
            System.out.println(maps);
            for (int i = 0; i <musicList.size() ; i++) {
                if (maps.get(0).get("musicId").equals(musicList.get(i).get("musicId"))){
                    currIndex=i;
                    System.out.println(musicList+"---"+currIndex);
                    break;
                }
                if (i==musicList.size()-1){
                    musicList.add(0,maps.get(0));
                    redisTemplate.opsForValue().set("musicList",musicList);
                    System.out.println(musicList+"---"+currIndex);
                    break;
                }

            }


        }


        System.out.println(musicList);
        if (0 <Integer.valueOf(userId)){

            List sheetName = favoriteService.selectSheetNameLisetByUserId(Integer.valueOf(userId));
            sheetName.add(0,"新建收藏歌单");

            Boolean sc=true;
            List<Map> objects = musicService.selectCollectionMusicByUserId(Integer.valueOf(userId));
            for (int i = 0; i <musicList.size() ; i++) {
                for (int j = 0; j <objects.size() ; j++) {
                    if (objects.get(j).get("musicId").equals(musicList.get(i).get("musicId"))){
                        sc=true;
                        break;
                    }else {
                        sc=false;
                    }
                }
                scList.add(sc);
            }
            System.out.println(scList);
            map.put("sheetName",sheetName);

        }

        map.put("scList",scList);
        map.put("musicList",musicList);
        map.put("currIndex",currIndex);
        JSONArray jsonArray=JSONArray.fromObject(map);

        try {
            resp.getWriter().print(jsonArray);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    @RequestMapping("/collection")
    public String collection(HttpSession session, HttpServletRequest req, HttpServletResponse resp, Model model){

        resp.setContentType("text/json");
        resp.setCharacterEncoding("utf-8");

        Integer musicId = Integer.valueOf(req.getParameter("musicId"));
        Integer userId = Integer.valueOf(req.getParameter("userId"));
        String newName = req.getParameter("newName");

        System.out.println(musicId);
        System.out.println(userId);
        System.out.println(newName);
        if (newName.equals("null")){
            System.out.println("取消");
            int row = favoriteService.cancelCollection(userId, musicId);
            System.out.println("row--"+row);
        }else {
            System.out.println("收藏");
            List sheetName = favoriteService.selectSheetNameLisetByUserId(Integer.valueOf(userId));
            for (int i = 0; i <sheetName.size() ; i++) {
                if (sheetName.get(i).equals(newName)){
                    System.out.println("已有歌单");
                    int row1 = favoriteService.songCollection(userId, musicId, newName);
                    break;
                }
                if (i==sheetName.size()-1){
                    System.out.println("新建歌单"+newName);
                    Date date=new Date();

                    int row2 = favoriteService.newCollectionSheet(userId, newName, date);
                    int row3 = favoriteService.songCollection(userId, musicId, newName);
                }
            }
        }

        List sheetName = favoriteService.selectSheetNameLisetByUserId(Integer.valueOf(userId));
        sheetName.add(0,"新建收藏歌单");


        Map<String,Object> map=new HashMap<>();
        List<Map> musicList = (List<Map>) redisTemplate.opsForValue().get("musicList");
        List<Boolean> scList=new ArrayList<>();
        Boolean sc=true;
        List<Map> objects = musicService.selectCollectionMusicByUserId(Integer.valueOf(userId));
        for (int i = 0; i <musicList.size() ; i++) {
            for (int j = 0; j <objects.size() ; j++) {
                if (objects.get(j).get("musicId").equals(musicList.get(i).get("musicId"))){
                    sc=true;
                    break;
                }else {
                    sc=false;
                }
            }
            scList.add(sc);
        }
        System.out.println(scList);
        map.put("scList",scList);
        map.put("sheetName",sheetName);

        JSONArray jsonArray=JSONArray.fromObject(map);

        try {
            resp.getWriter().print(jsonArray);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @RequestMapping("/visit")
    public String visit(){
        musicService.visit(new Date());
        return null;
    }

    @RequestMapping("/playbackVolume")
    public String playbackVolume(HttpSession session, HttpServletRequest req, HttpServletResponse resp, Model model){

        resp.setContentType("text/json");
        resp.setCharacterEncoding("utf-8");
        Integer musicId = Integer.valueOf(req.getParameter("musicId"));

        musicService.addPlaybackVolume(musicId);

        return null;
    }

    @RequestMapping("/search")
    public String search(HttpSession session, HttpServletRequest req, HttpServletResponse resp, Model model){

        resp.setContentType("text/json");
        resp.setCharacterEncoding("utf-8");
        String event = req.getParameter("event");
        List<Map> searchList=new ArrayList<>();
        System.out.println(event);

        if (!event.equals("")) {
            searchList = musicService.getSongListByMusicName(event);
            System.out.println(searchList);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("searchList",searchList);
        JSONArray jsonArray=JSONArray.fromObject(map);

        try {
            resp.getWriter().print(jsonArray);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
